﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WCrawler.Wap
{
    public partial class WapPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack){
                
                DataTable dtDownloadLink = new DataTable();
                DataColumn dc1 = new DataColumn("Name");
                DataColumn dc2 = new DataColumn("Url");
                dtDownloadLink.Columns.Add(dc1);
                dtDownloadLink.Columns.Add(dc2);
                Session["DownloadLink"] = dtDownloadLink;


                DataTable dtImageUrl = new DataTable();
                DataColumn dc = new DataColumn("Url");
                dtImageUrl.Columns.Add(dc);
                Session["ImageUrl"] = dtImageUrl;

                //DataRow dr1 = dtDownloadLink.NewRow();
                //GridViewDownloadLink.DataSource = dtDownloadLink;
                //GridViewDownloadLink.DataBind();
            }
        }

        protected void btnAddImage_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)Session["ImageUrl"];
            DataRow dr = dt.NewRow();
            //Image Url
            dr[0] = txtImage.Text;
            //Url
            dt.Rows.Add(dr);
            GridViewImages.DataSource = dt;
            GridViewImages.DataBind();
        }

        protected void btnAddDownloadLink_Click(object sender, EventArgs e)
        {
            //if (DropDownListType.SelectedIndex > 0)
            //{
            //    txtLinkName.Text =string.Format("Link tải về bản {0}", DropDownListType.SelectedItem.Text);
            //}
            //string imageDownload ="http://4.bp.blogspot.com/-dP2ZDPKpi2U/UAeMPtM4UrI/AAAAAAAAC_Y/Ffr1CFJCkow/s200/down.gif";
            //string downLink = string.Format("<a style=\"color:red;\" href=\"{0}\"><b><img src=\"{1}\">{2}</img></b></a><br/>",txtDownloadLink.Text, imageDownload, txtLinkName.Text);
            //txtViewSource.Text = downLink;

            DataTable dt = (DataTable)Session["DownloadLink"];
            DataRow dr1 = dt.NewRow();
            //Link name
            if (DropDownListType.SelectedIndex > 0)
                dr1[0] = txtLinkName.Text + " bản " + DropDownListType.SelectedValue;
            else
                dr1[0] = txtLinkName.Text;
            
            //Url
            dr1[1] = txtDownloadLink.Text;
            dt.Rows.Add(dr1);
            GridViewDownloadLink.DataSource = dt;
            GridViewDownloadLink.DataBind();
        }

        protected void btnAddContent_Click(object sender, EventArgs e)
        {
            //Add description
            txtViewSource.Text = txtContent.Text.Replace("\n", "<br/>\n");
            //Add images
            string imgUrl = "";
            DataTable dtImageUrl = (DataTable)Session["ImageUrl"];
            foreach (DataRow dr in dtImageUrl.Rows)
            {
                string url = dr[0].ToString();
                imgUrl += string.Format("<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"{0}\" imageanchor=\"1\" style=\"margin-left:1em; margin-right:1em\"><img border=\"0\" height=\"320\" width=\"240\" src=\"http://res.d.cn/i/gg2/34/1334/1298954050453slB5.jpg\" /></a></div>",url);
            }
            txtViewSource.Text += imgUrl;

            //Add download links
            string imageDownload = "http://4.bp.blogspot.com/-dP2ZDPKpi2U/UAeMPtM4UrI/AAAAAAAAC_Y/Ffr1CFJCkow/s200/down.gif";
            string downLink = "";
            DataTable dtDownloadUrl = (DataTable)Session["DownloadLink"];
            foreach (DataRow dr in dtDownloadUrl.Rows)
            {
                downLink += string.Format("<a style=\"color:red;\" href=\"{0}\"><b><img src=\"{1}\">{2}</img></b></a><br/>", dr["Url"].ToString(), imageDownload, dr["Name"].ToString());
            }
            txtViewSource.Text += downLink;
        }

        protected void GridViewDownloadLink_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable dt = (DataTable)Session["DownloadLink"];
            dt.Rows.RemoveAt(e.RowIndex);
            GridViewDownloadLink.DataSource = dt;
            GridViewDownloadLink.DataBind();
        }

        protected void GridViewImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable dt = (DataTable)Session["ImageUrl"];
            dt.Rows.RemoveAt(e.RowIndex);
            GridViewImages.DataSource = dt;
            GridViewImages.DataBind();
        }
    }
}